package l2ft.gameserver.handler.admincommands.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import l2ft.gameserver.database.DatabaseFactory;
import l2ft.gameserver.handler.admincommands.IAdminCommandHandler;
import l2ft.gameserver.model.Player;


@SuppressWarnings("unused")
public class AdminRepairChar implements IAdminCommandHandler
{
	private static enum Commands
	{
		admin_restore,
		admin_repair
	}

	@Override
	public boolean useAdminCommand(Enum comm, String[] wordList, String fullString, Player activeChar)
	{
		Commands command = (Commands) comm;

		if(activeChar.getPlayerAccess() == null || !activeChar.getPlayerAccess().CanEditChar)
			return false;

		if(wordList.length != 2)
			return false;

		try(Connection con = DatabaseFactory.getConnection())
		{
			try(PreparedStatement statement = con.prepareStatement("UPDATE characters SET x=-84318, y=244579, z=-3730 WHERE char_name=?"))
			{
				statement.setString(1, wordList[1]);
				statement.execute();
			}
			int objId = 0;
			try(PreparedStatement statement = con.prepareStatement("SELECT obj_id FROM characters where char_name=?"))
			{
				statement.setString(1, wordList[1]);
				try(ResultSet rset = statement.executeQuery())
				{
					if(rset.next())
						objId = rset.getInt(1);
				}
			}

			if(objId == 0)
				return false;
			
			try(PreparedStatement statement = con.prepareStatement("DELETE FROM character_shortcuts WHERE object_id=?"))
			{
				statement.setInt(1, objId);
				statement.execute();
			}

			try(PreparedStatement statement = con.prepareStatement("UPDATE items SET loc='INVENTORY' WHERE owner_id=? AND loc!='WAREHOUSE'"))
			{
				statement.setInt(1, objId);
				statement.execute();
			}

			try(PreparedStatement statement = con.prepareStatement("DELETE FROM character_variables WHERE obj_id=? AND `type`='user-var' AND `name`='reflection' LIMIT 1"))
			{
				statement.setInt(1, objId);
				statement.execute();
			}
		}
		catch(Exception e)
		{

		}

		return true;
	}

	@Override
	public Enum[] getAdminCommandEnum()
	{
		return Commands.values();
	}
}